<template>
  <div class="login-bg">
    <div class="login-container">
      <div class="login-left">
        <div class="login-left-content">
          <h2>Welcome to</h2>
          <h1>招标文件智析系统</h1>
          <p>你的强有力招标文件分析工具</p>
        </div>
      </div>
      <div class="login-right">
        <h2>新密码</h2>
        <form @submit.prevent="handleChangePassword">
          <div v-if="showAccountError" class="error-tip">此账号不存在，请先去注册</div>
          <div v-if="changeSuccess" class="success-tip">{{ successMsg }}</div>
          
          <!-- 显示手机号（只读） -->
          <div class="phone-display">
            <label>手机号：</label>
            <span>{{ phone }}</span>
          </div>
          
          <div class="password-box">
            <input
              :type="showNewPassword ? 'text' : 'password'"
              v-model="newPassword"
              placeholder="请输入新密码"
              @input="newPasswordTouched = true"
            />
            <span class="toggle" @click="showNewPassword = !showNewPassword">
              <img 
                :src="showNewPassword ? eyeShowIcon : eyeHideIcon" 
                alt="toggle new password"
                class="eye-icon"
              />
            </span>
          </div>
          <div v-if="newPasswordTouched && newPasswordInvalid" style="color: #000; font-size: 0.85rem; margin-bottom: 8px;">
            密码至少6位，且必须包含字母和数字
          </div>
          <div v-if="showPasswordError" class="error-tip">密码至少6位，且必须包含字母和数字！</div>
          <!-- 新密码提示 -->
          <div v-if="showPasswordTip" style="color: #000; font-size: 0.85rem; margin-bottom: 8px;">
            {{ passwordTipMsg }}
          </div>
          
          <div class="password-box">
            <input
              :type="showConfirmPassword ? 'text' : 'password'"
              v-model="confirmPassword"
              placeholder="请确认新密码"
              @input="confirmTouched = true"
            />
            <span class="toggle" @click="showConfirmPassword = !showConfirmPassword">
              <img 
                :src="showConfirmPassword ? eyeShowIcon : eyeHideIcon" 
                alt="toggle confirm password"
                class="eye-icon"
              />
            </span>
          </div>
          <div v-if="showConfirmError" style="color: #000; background: none; border: none; margin-top: 8px; text-align: center;">
            {{ confirmErrorMsg }}
          </div>
          <!-- 确认密码提示 -->
          <div v-if="showConfirmTip" style="color: #000; font-size: 0.95rem; margin-bottom: 8px;">
            {{ confirmTipMsg }}
          </div>
          
          <button type="submit">确认修改</button>
          <div v-if="showPhoneError" style="color: #000; background: none; border: none; margin-top: 8px; text-align: center;">
            {{ phoneErrorMsg }}
          </div>
        </form>
        <div class="login-footer">
          <router-link to="/login">返回登录</router-link>
          <span> | </span>
          <a href="#" @click.prevent="goBackToLogin">取消修改</a>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import eyeShowIcon from '@/assets/icons/眼睛_显示.svg';
import eyeHideIcon from '@/assets/icons/眼睛_隐藏.svg';

export default {
  name: "ChangePasswordPage",
  data() {
    return {
      phone: "",
      newPassword: "",
      confirmPassword: "",
      showNewPassword: false,
      showConfirmPassword: false,
      eyeShowIcon,
      eyeHideIcon,
      newPasswordTouched: false,
      confirmTouched: false,
      changeSuccess: false,
      successMsg: "",
      accountNotExist: false,
      showPhoneError: false,
      showPasswordError: false,
      showConfirmError: false,
      showAccountError: false,
      phoneErrorMsg: "",
      showPhoneTip: false,
      phoneTipMsg: '',
      phoneTipTimer: null,
      showPasswordTip: false,
      passwordTipMsg: '',
      passwordTipTimer: null,
      showConfirmTip: false,
      confirmTipMsg: '',
      confirmTipTimer: null,
      confirmErrorMsg: '',
    };
  },
  computed: {
    newPasswordInvalid() {
      if (!this.newPasswordTouched) return false;
      const pwdReg = /^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{6,}$/;
      return !pwdReg.test(this.newPassword);
    },
    confirmInvalid() {
      if (!this.confirmTouched) return false;
      return this.confirmPassword !== this.newPassword;
    }
  },
  mounted() {
    // 检查是否从找回密码页面跳转过来
    const canChangePassword = sessionStorage.getItem('canChangePassword');
    const changePhone = sessionStorage.getItem('changePhone');
    
    if (canChangePassword === '1' && changePhone) {
      this.phone = changePhone;
      // 清除sessionStorage中的标记
      sessionStorage.removeItem('canChangePassword');
      sessionStorage.removeItem('changePhone');
    } else {
      // 如果没有正确的跳转标记，返回找回密码页面
      this.$router.push('/forgot-password');
    }
  },
  methods: {
    showError(type, message) {
      // 隐藏所有错误提示
      this.showPasswordError = false;
      this.showConfirmError = false;
      this.showAccountError = false;
      
      // 显示指定类型的错误提示
      switch(type) {
        case 'password':
          this.showPasswordError = true;
          break;
        case 'confirm':
          this.showConfirmError = true;
          this.confirmErrorMsg = message;
          break;
        case 'account':
          this.showAccountError = true;
          break;
      }
      
      // 2秒后自动隐藏
      setTimeout(() => {
        this.showPasswordError = false;
        this.showConfirmError = false;
        this.showAccountError = false;
      }, 2000);
    },
    handleChangePassword() {
      if (!this.newPasswordTouched) this.newPasswordTouched = true;
      if (!this.confirmTouched) this.confirmTouched = true;
      
      let hasError = false;
      
      if (!this.newPassword) {
        this.showError('password', '请输入新密码');
        hasError = true;
      } else if (this.newPasswordInvalid) {
        this.showError('password', '密码至少6位，且必须包含字母和数字');
        hasError = true;
      }
      
      if (!this.confirmPassword) {
        this.showError('confirm', '请确认新密码');
        hasError = true;
      } else if (this.confirmInvalid) {
        this.showError('confirm', '两次输入的密码不一致');
        hasError = true;
      }
      
      if (hasError) {
        return;
      }
      
      // 检查手机号是否存在
      const registeredPhones = JSON.parse(localStorage.getItem('registeredPhones') || '[]');
      if (!registeredPhones.includes(this.phone)) {
        this.showAccountError = true;
        return;
      }
      
      // 保存新密码
      let userPasswords = JSON.parse(localStorage.getItem('userPasswords') || '{}');
      userPasswords[this.phone] = this.newPassword;
      localStorage.setItem('userPasswords', JSON.stringify(userPasswords));
      
      // 显示成功消息
      this.changeSuccess = true;
      this.successMsg = "密码修改成功！";
      
      // 2秒后跳转到登录页
      setTimeout(() => {
        this.$router.push('/login');
      }, 2000);
    },
    goBackToLogin() {
      this.$router.push('/login');
    },
    triggerPasswordTip(msg) {
      if (this.passwordTipTimer) clearTimeout(this.passwordTipTimer);
      this.passwordTipMsg = msg;
      this.showPasswordTip = true;
      this.passwordTipTimer = setTimeout(() => {
        this.showPasswordTip = false;
      }, 2000);
    },
    triggerConfirmTip(msg) {
      if (this.confirmTipTimer) clearTimeout(this.confirmTipTimer);
      this.confirmTipMsg = msg;
      this.showConfirmTip = true;
      this.confirmTipTimer = setTimeout(() => {
        this.showConfirmTip = false;
      }, 2000);
    },
  },
  beforeUnmount() {
    // 清理定时器
    if (this.phoneTipTimer) clearTimeout(this.phoneTipTimer);
    if (this.passwordTipTimer) clearTimeout(this.passwordTipTimer);
    if (this.confirmTipTimer) clearTimeout(this.confirmTipTimer);
  }
};
</script>

<style scoped>
.login-bg {
  min-height: 100vh;
  background: url('../assets/img/background.png') no-repeat center center;
  background-size: cover;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
}

.login-bg::before {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(255, 255, 255, 0.3);
  backdrop-filter: blur(5px);
  z-index: 1;
}

.login-container {
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  margin-top: -80px;
  margin-left: -80px;
  z-index: 2;
}

.login-left, .login-right {
  min-height: unset;
  height: auto;
  overflow: hidden;
}

.login-left {
  background: #5a6b7f;
  color: #fff;
  border-radius: 80px;
  padding: 60px 60px 60px 60px;
  width: 900px;
  height: 380px;
  margin-right: -120px;
  box-shadow: 0 8px 32px rgba(0,0,0,0.10);
  display: flex;
  flex-direction: column;
  justify-content: center;
  z-index: 1;
  position: relative;
}

.login-left-content {
  position: relative;
  z-index: 2;
  text-align: left;
}

.login-left h2 {
  font-size: 3rem;
  margin-bottom: 22px;
  font-weight: 700;
}

.login-left h1 {
  font-size: 3.2rem;
  margin-bottom: 22px;
  font-weight: 700;
}

.login-left p {
  font-size: 1.2rem;
  opacity: 0.9;
}

.login-right {
  background: #d4c4a8;
  border-radius: 80px;
  padding: 60px 40px;
  width: 320px;
  height: 580px;
  box-shadow: 0 8px 32px rgba(0,0,0,0.10);
  display: flex;
  flex-direction: column;
  align-items: center;
  z-index: 2;
  position: relative;
  margin-left: -380px;
}

.login-right h2 {
  text-align: center;
  margin-bottom: 40px;
  font-size: 2.3rem;
  font-weight: 800;
  color: #fff;
  letter-spacing: 2px;
}

form {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

input {
  padding: 16px 20px;
  border: 2px solid #e1e5e9;
  border-radius: 12px;
  font-size: 1rem;
  transition: all 0.3s ease;
  background: #f8f9fa;
  width: 100%;
  box-sizing: border-box;
}

input:focus {
  outline: none;
  border-color: #7b90a7;
  background: #fff;
  box-shadow: 0 0 0 3px rgba(123, 144, 167, 0.1);
}

input[type="text"] {
  width: 100%;
  margin-left: 0;
  margin-right: 0;
}

.verify-box {
  position: relative;
  display: flex;
  gap: 10px;
  width: 100%;
}

.verify-box input {
  flex: 1;
  min-width: 0;
}

.verify-btn {
  padding: 16px 20px;
  background: #7b90a7;
  color: white;
  border: none;
  border-radius: 12px;
  cursor: pointer;
  font-size: 0.9rem;
  white-space: nowrap;
  transition: all 0.3s ease;
}

.verify-btn:hover:not(:disabled) {
  background: #6a7f95;
}

.verify-btn:disabled {
  background: #ccc;
  cursor: not-allowed;
}

.code-tip {
  position: absolute;
  top: -25px;
  left: 0;
  font-size: 0.8rem;
  color: #28a745;
}

.password-box {
  position: relative;
  width: 100%;
}

.password-box input {
  width: 100%;
  padding-right: 50px;
  margin-left: 0;
  margin-right: 0;
}

.toggle {
  position: absolute;
  right: 16px;
  top: 50%;
  transform: translateY(-50%);
  cursor: pointer;
  user-select: none;
  font-size: 1.2rem;
}

button[type="submit"] {
  padding: 16px 20px;
  background: #7b90a7;
  color: white;
  border: none;
  border-radius: 12px;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
  margin-top: 20px;
  width: 100%;
}

button[type="submit"]:hover {
  background: #6a7f95;
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(123, 144, 167, 0.3);
}

.login-footer {
  margin-top: 30px;
  text-align: center;
  font-size: 0.9rem;
}

.login-footer a {
  color: #7b90a7;
  text-decoration: none;
  font-weight: 500;
}

.login-footer a:hover {
  text-decoration: underline;
}

.error-tip {
  color: #e74c3c;
  background: #fbeaea;
  border-radius: 6px;
  padding: 12px;
  margin-top: -10px;
  margin-bottom: 15px;
  font-size: 0.9rem;
  text-align: center;
}

.success-tip {
  color: #2ecc40;
  background: #eafbe7;
  border-radius: 6px;
  padding: 12px;
  margin-bottom: 15px;
  font-size: 0.9rem;
  text-align: center;
}

.phone-display {
  background: #f5f5f5;
  border: 1px solid #ddd;
  border-radius: 8px;
  padding: 12px 16px;
  margin-bottom: 20px;
  display: flex;
  align-items: center;
}

.phone-display label {
  font-weight: 600;
  margin-right: 8px;
  color: #333;
}

.phone-display span {
  color: #666;
  font-size: 0.95rem;
}

input[type="password"]::-ms-reveal,
input[type="password"]::-ms-clear {
  display: none;
}

input[type="password"]::-webkit-credentials-auto-fill-button,
input[type="password"]::-webkit-input-password-toggle-button,
input[type="password"]::-webkit-input-password-toggle {
  display: none !important;
}

.eye-icon {
  width: 20px;
  height: 20px;
  cursor: pointer;
  transition: opacity 0.2s ease;
}

.eye-icon:hover {
  opacity: 0.7;
}
</style> 